using System;
using System.Collections.Generic;
using RpgXna.Model.Menu;
using RpgXna.Model.Menu.MenuBehaviors;
using RpgXna.Model.XnaClassesWrappers;

namespace RpgXna.Model.Interfaces.ContextMenu.NavigationBehaviors
{
    class ContextMenuValidateBehavior : KeysInputBehavior, IContextMenuNavigateBehavior
    {
        private readonly Action _callback;

        public ContextMenuValidateBehavior(Func<bool> predicate, Action callback = null)
        {
            _callback = callback;
            Predicate = predicate;
        }

        public void Execute(List<INavigationSelectable> options, IKeyboardStateWrapper kbs)
        {
            if (!Predicate()) return;

            CurKbs = kbs;
            if (KeyPressed(InputBindings.Bindings.Validate))
            {
                foreach (IContextMenuBehavior mb in options)
                {
                    if (mb.IsSelected)
                    {
                        mb.Execute();
                        _callback();
                        break;
                    }
                }
            }
            Prevkbs = kbs;
        }
    }
}